/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmPrincipal.java
 *
 * Created on 02/06/2011, 05:21:47
 */

package br.hostess.view;

import br.hostess.controller.cntrManterEvento;
import br.hostess.controller.cntrEfetivarRegistro;
import br.hostess.controller.cntrManterParticipante;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author Marcelo Daibert
 */
public class frmPrincipal extends javax.swing.JFrame {
    private ArrayList<String> vetIDEventosCombo;
    /** Creates new form frmPrincipal */
    public frmPrincipal() {
        initComponents();
        Dimension objDim = this.getToolkit().getScreenSize();
        Rectangle abounds = this.getBounds();
        this.setLocation((objDim.width - abounds.width) / 2, (objDim.height - abounds.height) / 2);
        this.requestFocus();
        this.jLabelRegistradoSucesso.setVisible(false);
        this.jLabelNomeParticipante.setVisible(false);
        this.vetIDEventosCombo = new ArrayList<String>();
        this.atualizaCombo();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jToolBarPrincipal = new javax.swing.JToolBar();
        jButtonEvento = new javax.swing.JButton();
        jButtonParticipantes = new javax.swing.JButton();
        jPanelPrincipal = new javax.swing.JPanel();
        jLabelEvento = new javax.swing.JLabel();
        jComboBoxEvento = new javax.swing.JComboBox();
        jButtonRegistrar = new javax.swing.JButton();
        jLabelCodigo = new javax.swing.JLabel();
        jTextFieldRegistro = new javax.swing.JTextField();
        jButtonManual = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabelRegistradoSucesso = new javax.swing.JLabel();
        jLabelNomeParticipante = new javax.swing.JLabel();
        jButtonAtualizarCombo = new javax.swing.JButton();
        jMenuBar = new javax.swing.JMenuBar();
        jMenuCadastros = new javax.swing.JMenu();
        jMenuItemEvento = new javax.swing.JMenuItem();
        jMenuItemParticipantes = new javax.swing.JMenuItem();
        jMenuRelatorios = new javax.swing.JMenu();
        jMenuAjuda = new javax.swing.JMenu();
        jMenuItemSobre = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("HostessControl");
        setAlwaysOnTop(true);
        setResizable(false);

        jToolBarPrincipal.setFloatable(false);
        jToolBarPrincipal.setRollover(true);

        jButtonEvento.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/ark.png"))); // NOI18N
        jButtonEvento.setText("Evento");
        jButtonEvento.setFocusable(false);
        jButtonEvento.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonEvento.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonEvento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEventoActionPerformed(evt);
            }
        });
        jToolBarPrincipal.add(jButtonEvento);

        jButtonParticipantes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/ark2.png"))); // NOI18N
        jButtonParticipantes.setText("Participantes");
        jButtonParticipantes.setFocusable(false);
        jButtonParticipantes.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonParticipantes.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonParticipantes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonParticipantesActionPerformed(evt);
            }
        });
        jToolBarPrincipal.add(jButtonParticipantes);

        jLabelEvento.setText("Evento:");

        jButtonRegistrar.setText("Registrar Presença");
        jButtonRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegistrarActionPerformed(evt);
            }
        });

        jLabelCodigo.setFont(new java.awt.Font("Tahoma", 0, 36));
        jLabelCodigo.setText("Código do Participante:");

        jTextFieldRegistro.setFont(new java.awt.Font("Tahoma", 0, 48));
        jTextFieldRegistro.setEnabled(false);
        jTextFieldRegistro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldRegistroKeyPressed(evt);
            }
        });

        jButtonManual.setText("Registrar Manual");
        jButtonManual.setEnabled(false);
        jButtonManual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonManualActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Status"));

        jLabelRegistradoSucesso.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabelRegistradoSucesso.setText("Registrado com Sucesso");

        jLabelNomeParticipante.setFont(new java.awt.Font("Tahoma", 0, 40));
        jLabelNomeParticipante.setForeground(new java.awt.Color(14, 78, 133));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(737, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelNomeParticipante, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabelRegistradoSucesso, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(41, Short.MAX_VALUE)
                .addComponent(jLabelNomeParticipante)
                .addGap(37, 37, 37)
                .addComponent(jLabelRegistradoSucesso)
                .addContainerGap())
        );

        jButtonAtualizarCombo.setText("Atualizar Combo");
        jButtonAtualizarCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAtualizarComboActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelPrincipalLayout = new javax.swing.GroupLayout(jPanelPrincipal);
        jPanelPrincipal.setLayout(jPanelPrincipalLayout);
        jPanelPrincipalLayout.setHorizontalGroup(
            jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPrincipalLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelPrincipalLayout.createSequentialGroup()
                            .addGroup(jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButtonManual, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jTextFieldRegistro, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 768, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(89, 89, 89))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelPrincipalLayout.createSequentialGroup()
                            .addGroup(jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanelPrincipalLayout.createSequentialGroup()
                                    .addGroup(jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jButtonAtualizarCombo)
                                        .addGroup(jPanelPrincipalLayout.createSequentialGroup()
                                            .addComponent(jLabelEvento)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(jComboBoxEvento, javax.swing.GroupLayout.PREFERRED_SIZE, 748, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGap(18, 18, 18)
                                    .addComponent(jButtonRegistrar, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)))
                            .addContainerGap()))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelPrincipalLayout.createSequentialGroup()
                        .addComponent(jLabelCodigo)
                        .addGap(271, 271, 271))))
        );
        jPanelPrincipalLayout.setVerticalGroup(
            jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPrincipalLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelEvento)
                    .addComponent(jComboBoxEvento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonRegistrar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonAtualizarCombo)
                .addGap(34, 34, 34)
                .addComponent(jLabelCodigo)
                .addGap(18, 18, 18)
                .addComponent(jTextFieldRegistro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonManual)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jMenuCadastros.setMnemonic('C');
        jMenuCadastros.setText("Cadastros");

        jMenuItemEvento.setMnemonic('E');
        jMenuItemEvento.setText("Eventos");
        jMenuItemEvento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemEventoActionPerformed(evt);
            }
        });
        jMenuCadastros.add(jMenuItemEvento);

        jMenuItemParticipantes.setMnemonic('P');
        jMenuItemParticipantes.setText("Participantes");
        jMenuItemParticipantes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemParticipantesActionPerformed(evt);
            }
        });
        jMenuCadastros.add(jMenuItemParticipantes);

        jMenuBar.add(jMenuCadastros);

        jMenuRelatorios.setMnemonic('R');
        jMenuRelatorios.setText("Relatórios");
        jMenuBar.add(jMenuRelatorios);

        jMenuAjuda.setMnemonic('A');
        jMenuAjuda.setText("Ajuda");

        jMenuItemSobre.setMnemonic('S');
        jMenuItemSobre.setText("Sobre");
        jMenuAjuda.add(jMenuItemSobre);

        jMenuBar.add(jMenuAjuda);

        setJMenuBar(jMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBarPrincipal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 970, Short.MAX_VALUE)
            .addComponent(jPanelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBarPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void atualizaCombo(){
        jComboBoxEvento.removeAllItems();
        ArrayList<ArrayList> vetRecebe = cntrManterEvento.recuperaObjetosCombo();
        this.vetIDEventosCombo = vetRecebe.get(0);
        ArrayList<String> vetDescricoesCombo = vetRecebe.get(1);
        String vBuffer;
        for (int i = 0; i < vetDescricoesCombo.size(); i++){
            vBuffer = vetDescricoesCombo.get(i).toString();
            jComboBoxEvento.addItem(vBuffer);
        }
    }

    private void jButtonManualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonManualActionPerformed
        //JOptionPane.showMessageDialog(this, "Submetido");
        //JOptionPane.showMessageDialog(this, "Resultado: "+jComboBoxEvento.getSelectedIndex());
        //JOptionPane.showMessageDialog(this, "Resultado: "+this.vetIDEventosCombo.get(jComboBoxEvento.getSelectedIndex()));
        String vCodigoEvento = this.vetIDEventosCombo.get(jComboBoxEvento.getSelectedIndex());
        
        //Tratar com substring os números
        
        String vCodigo = jTextFieldRegistro.getText().trim();
        /*if (vCodigo.length() > 6){
            vCodigo = vCodigo.substring(2, 11);
            int vCodigoFormatado = Integer.parseInt(vCodigo);
            vCodigo = String.valueOf(vCodigoFormatado);    
        }*/
        int vCodigoFormatado = Integer.parseInt(vCodigo);
        vCodigo = String.valueOf(vCodigoFormatado);                    
        //JOptionPane.showMessageDialog(this, "Resultado: "+vCodigo);        
        Calendar objCalendario = Calendar.getInstance();
        Date objAgora = objCalendario.getTime();
        //JOptionPane.showMessageDialog(this, "Hora: "+objAgora.getTime());
        if(cntrManterParticipante.existeParticipante(vCodigo)){
            cntrEfetivarRegistro objController = new cntrEfetivarRegistro();
            if (objController.registrarEntradaSaida(vCodigo, vCodigoEvento)){
                String vNome = cntrManterParticipante.recuperaObjetoNome(vCodigo);
                jLabelNomeParticipante.setText(vNome);
                Color objColor = new Color(14, 78, 133);
                jLabelNomeParticipante.setForeground(objColor);
                jLabelNomeParticipante.setVisible(true);
                jLabelRegistradoSucesso.setText("Registrado com Sucesso - "+objAgora.toString());
                jLabelRegistradoSucesso.setVisible(true);
            }else{
                jLabelNomeParticipante.setText("Erro. Contate o Administrador");
                jLabelNomeParticipante.setForeground(Color.RED);
                jLabelNomeParticipante.setVisible(true);            
            }
        }else{
            jLabelNomeParticipante.setText("Participante não cadastrado");
            jLabelNomeParticipante.setForeground(Color.RED);
            jLabelNomeParticipante.setVisible(true); 
            jLabelRegistradoSucesso.setVisible(false);
        }
        
        
        this.jTextFieldRegistro.setText("");
    }//GEN-LAST:event_jButtonManualActionPerformed

    private void jTextFieldRegistroKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldRegistroKeyPressed
        int vKey = evt.getKeyCode();
        if (vKey == KeyEvent.VK_ENTER){
            this.jButtonManualActionPerformed(null);
        }
    }//GEN-LAST:event_jTextFieldRegistroKeyPressed

    private void jButtonEventoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEventoActionPerformed
        new frmPesquisaEvento().setVisible(true);

    }//GEN-LAST:event_jButtonEventoActionPerformed

    private void jButtonRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegistrarActionPerformed
        this.jComboBoxEvento.setEnabled(false);
        this.jTextFieldRegistro.setEnabled(true);
        this.jButtonManual.setEnabled(true);
    }//GEN-LAST:event_jButtonRegistrarActionPerformed

    private void jButtonAtualizarComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAtualizarComboActionPerformed
        this.atualizaCombo();
    }//GEN-LAST:event_jButtonAtualizarComboActionPerformed

    private void jMenuItemEventoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemEventoActionPerformed
        new frmPesquisaEvento().setVisible(true);
    }//GEN-LAST:event_jMenuItemEventoActionPerformed

    private void jMenuItemParticipantesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemParticipantesActionPerformed
        new frmPesquisaParticipante().setVisible(true);
    }//GEN-LAST:event_jMenuItemParticipantesActionPerformed

    private void jButtonParticipantesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonParticipantesActionPerformed
        new frmPesquisaParticipante().setVisible(true);
    }//GEN-LAST:event_jButtonParticipantesActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmPrincipal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAtualizarCombo;
    private javax.swing.JButton jButtonEvento;
    private javax.swing.JButton jButtonManual;
    private javax.swing.JButton jButtonParticipantes;
    private javax.swing.JButton jButtonRegistrar;
    private javax.swing.JComboBox jComboBoxEvento;
    private javax.swing.JLabel jLabelCodigo;
    private javax.swing.JLabel jLabelEvento;
    private javax.swing.JLabel jLabelNomeParticipante;
    private javax.swing.JLabel jLabelRegistradoSucesso;
    private javax.swing.JMenu jMenuAjuda;
    private javax.swing.JMenuBar jMenuBar;
    private javax.swing.JMenu jMenuCadastros;
    private javax.swing.JMenuItem jMenuItemEvento;
    private javax.swing.JMenuItem jMenuItemParticipantes;
    private javax.swing.JMenuItem jMenuItemSobre;
    private javax.swing.JMenu jMenuRelatorios;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanelPrincipal;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JTextField jTextFieldRegistro;
    private javax.swing.JToolBar jToolBarPrincipal;
    // End of variables declaration//GEN-END:variables

}
